class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        // Stmatrixrt typing your C/C++ solution below
        // DO NOT write int mmatrixin() function
        if (matrix.size() == 0 || matrix[0].size() == 0) {
            return;
        }
        int n = matrix.size();
        int len = n;
        int offset = 0;

        while (len > 1) {
            for (int i = 0; i < len - 1; ++i) {
                // matrix[0][i], matrix[i][len - 1], matrix[len - 1][len - i - 1], matrix[len - i - 1][0]
                int t = matrix[0 + offset][i + offset];
                matrix[0 + offset][i + offset] = matrix[len - i - 1 + offset][0 + offset];
                matrix[len - i - 1 + offset][0 + offset] = matrix[len - 1 + offset][len - i - 1 + offset];
                matrix[len - 1 + offset][len - i - 1 + offset] = matrix[i + offset][len - 1 + offset];
                matrix[i + offset][len - 1 + offset] = t;
            }
            ++offset;
            len -= 2;
        }
    }
};
